﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Microsoft.Reporting.WebForms;
using System.Net;
using System.Security.Principal;
using Crosland.Automotriz.Logic.ControlExeption;
namespace Crosland.Automotriz.Web.Repuestos
{
    public partial class wFrmProcesarArticulosABCReporte : System.Web.UI.Page
    {
        string idNegocio = ConfigurationManager.AppSettings["Negocio"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", "JavaScript:confirmSubmitResult();", true);
                mostrarReport();
                lblNumProceso.Text = Session["NUM_ANIO_MES"].ToString();

            }
        }

        void mostrarReport()
        {

            try
            {
                int num_proc = Convert.ToInt32(Session["NUM_PROC"].ToString());
                int num_mes = Convert.ToInt32(Session["NUM_MES"]);
                int num_anio = Convert.ToInt32(Session["NUM_ANIO"]);
                ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerCredentials();
                ReportViewer1.ProcessingMode = ProcessingMode.Remote;
                ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["RsServer"]);
                ReportViewer1.ServerReport.ReportPath = "/SAP_Repuestos/rsInventarioABCMatrizImporteCant";

                ReportParameter parametro = new ReportParameter("num_proc", num_proc.ToString());
                ReportViewer1.ServerReport.SetParameters(parametro);

                ReportParameter parametro2 = new ReportParameter("MES", num_mes.ToString());
                ReportViewer1.ServerReport.SetParameters(parametro2);

                ReportParameter parametro3 = new ReportParameter("ANIO", num_anio.ToString());
                ReportViewer1.ServerReport.SetParameters(parametro3);



                //ReportParameter[] parametro = new ReportParameter[1];
                //parametro[0] = new ReportParameter();
                //parametro[0].Name = "num_proc";
                //parametro[0].Values.Add(Convert.ToString(num_proc));
                //parametro[0].Visible = false;
      
                ReportViewer1.ServerReport.Refresh();

            }
            catch (RunException exc)
            {
                GuardarExceptionLog(exc.Message, exc, "mostrarReport");
                MensajesError(exc.Message, exc, "mostrarReport");
            }




        }



        [Serializable]
        public sealed class MyReportServerCredentials : IReportServerCredentials
        {
            public WindowsIdentity ImpersonationUser
            {
                get
                {

                    return null;
                }
            }

            public ICredentials NetworkCredentials
            {
                get
                {


                    // User name
                    string userName = ConfigurationManager.AppSettings["RSUser"];

                    if (string.IsNullOrEmpty(userName))
                        throw new Exception(
                            "Missing user name from web.config file");

                    // Password
                    string password =
                        ConfigurationManager.AppSettings["RSPassword"];

                    if (string.IsNullOrEmpty(password))
                        throw new Exception(
                            "Missing password from web.config file");

                    // Domain
                    string domain =
                        ConfigurationManager.AppSettings["RSDomain"];

                    if (string.IsNullOrEmpty(domain))
                        throw new Exception(
                            "Missing domain from web.config file");

                    return new NetworkCredential(userName, password, domain);
                }
            }

            public bool GetFormsCredentials(out Cookie authCookie,
                        out string userName, out string password,
                        out string authority)
            {
                authCookie = null;
                userName = null;
                password = null;
                authority = null;


                return false;
            }


        }

        protected void btnAtras_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Repuestos/wFrmProcesarArticulosABC.aspx?id=" + idNegocio);
        }


        private void GuardarExceptionLog(String msgUsu, Exception ex, String metError)
        {
            try
            {
                String msgReal, fhError, pagError, codUsu;
                msgReal = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
                pagError = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
                codUsu = Session["User"].ToString();
                fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");

                System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
                int number = 0;
                for (int i = 0; i < trace.FrameCount; i++)
                {
                    System.Diagnostics.StackFrame sf = trace.GetFrame(i);
                    number = sf.GetFileLineNumber();
                }
                fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
                new Logg().ErrorLog(Server.MapPath("~/Logs/ErrorLog.csv"), msgUsu, msgReal, pagError, metError, codUsu, number);
            }
            catch (DataException e) { MensajesError(e.Message, e, "GuardarExceptionLog"); }
            catch (RunException re) { MensajesError(re.Message, re, "GuardarExceptionLog"); }

        }

        private void MensajesError(String msgUsu, Exception ex, String metError)
        {
            Crosland.Automotriz.Web.UserControl.MensajeError uc = (Crosland.Automotriz.Web.UserControl.MensajeError)Master.FindControl("ucErrorMensaje");
            String msgReal, fhError, pagError, codUsu;
            msgReal = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
            pagError = new System.IO.FileInfo(Page.Request.Url.AbsolutePath).Name;
            codUsu = Session["User"].ToString();
            fhError = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
            System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
            int number = 0;
            for (int i = 0; i < trace.FrameCount; i++)
            {
                System.Diagnostics.StackFrame sf = trace.GetFrame(i);
                number = sf.GetFileLineNumber();
            }
            uc.Errores(msgUsu, msgReal, fhError, pagError, metError, codUsu, number);
        }


    }
}